<?php

namespace mbilling\BillingBundle\Model;

use Doctrine\ORM\EntityManager;
use mbilling\BillingBundle\Entity\ordersShared;


Class OrdersSharedModel
{
    protected $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function findAll()
    {
        $orders = $this->em->getRepository('BillingBundle:ordersShared')->findAll();
        return $orders;
    }
    
    public function findByUserId($user_id)
    {
        $query = $this->em->createQuery('SELECT oS FROM BillingBundle:ordersShared oS WHERE oS.order_id = ANY
                                    (SELECT o FROM BillingBundle:orders o WHERE o.is_active = 1 and o.user_id = '.$user_id.')');
        $orders = $query->getResult();
        return $orders;
    }

    public function findByOrderName($order_name)
    {
        $order = $this->em->getRepository('BillingBundle:ordersShared')->findOneBy(array('unix_login' => $order_name));
        return $order;
    }
    
    public function findByOrderId($order_id)
    {
        $order = $this->em->getRepository('BillingBundle:ordersShared')->findOneBy(array('order_id' => $order_id));
        return $order;
    }
    
    public function findByPK($order_id)
    {
        $order = $this->em->find('BillingBundle:ordersShared', $order_id);
        return $order;
    }

    public function create($oEntity, $tarif, $order)
    {
        $oEntity->setHdd($tarif->getHdd());
        $oEntity->setCronAccess($tarif->getCronAccess());
        $oEntity->setFtpAccess($tarif->getFtpAccess());
        $oEntity->setDnsAccess($tarif->getDnsAccess());
        $oEntity->setEmailAccess($tarif->getEmailAccess());
        $oEntity->setMysqlAccess($tarif->getMysqlAccess());
        $oEntity->setBackupAccess($tarif->getBackupAccess());
        $oEntity->setSshAccess($tarif->getSshAccess());
        $oEntity->setSslAccess($tarif->getSslAccess());
        $oEntity->setCgiBinAccess($tarif->getCgiBinAccess());
        $oEntity->setPerlPhpAccess($tarif->getPerlPhpAccess());
        $oEntity->setAnonymousFtpAccess($tarif->getAnonymousFtpAccess());
        $oEntity->setFtpCount($tarif->getFtpCount());
        $oEntity->setMysqlCount($tarif->getMysqlCount());
        $oEntity->setSiteCount($tarif->getSiteCount());
        $oEntity->setTraficCount($tarif->getTraficCount());
        $oEntity->setEmailCount($tarif->getEmailCount());
        $oEntity->setDomainCount($tarif->getDomainCount());
        $oEntity->setIsChanged('0');
        $oEntity->setOrderId($order->getId());
        $oEntity->setSharedTarif($tarif->getId());
        $this->em->persist($oEntity);
        $this->em->flush();
    }
    
    public function update($oEntity, $user, $tarif, $order)
    {
        if (!$user->isAdmin())
        {
            $oEntity->setHdd($tarif->getHdd());
            $oEntity->setCronAccess($tarif->getCronAccess());
            $oEntity->setFtpAccess($tarif->getFtpAccess());
            $oEntity->setDnsAccess($tarif->getDnsAccess());
            $oEntity->setEmailAccess($tarif->getEmailAccess());
            $oEntity->setMysqlAccess($tarif->getMysqlAccess());
            $oEntity->setBackupAccess($tarif->getBackupAccess());
            $oEntity->setSshAccess($tarif->getSshAccess());
            $oEntity->setSslAccess($tarif->getSslAccess());
            $oEntity->setCgiBinAccess($tarif->getCgiBinAccess());
            $oEntity->setPerlPhpAccess($tarif->getPerlPhpAccess());
            $oEntity->setAnonymousFtpAccess($tarif->getAnonymousFtpAccess());
            $oEntity->setFtpCount($tarif->getFtpCount());
            $oEntity->setMysqlCount($tarif->getMysqlCount());
            $oEntity->setSiteCount($tarif->getSiteCount());
            $oEntity->setTraficCount($tarif->getTraficCount());
            $oEntity->setEmailCount($tarif->getEmailCount());
            $oEntity->setDomainCount($tarif->getDomainCount());
            $oEntity->setIsChanged('0');  
        }
        $oEntity->setOrderId($order->getId());
        $oEntity->setSharedTarif($tarif->getId());
        $this->em->persist($oEntity);
        $this->em->flush();
    }

    public function simple_update($oEntity)
    {
        $this->em->persist($oEntity);
        $this->em->flush();
    }
    
    public function removeByPK($order_id)
    {
        $order = $this->em->find('BillingBundle:ordersShared', $order_id);
        $this->em->remove($order);
        $this->em->flush();
    }

    public function removeByOrderId($order_id)
    {
        $order = $this->em->getRepository('BillingBundle:ordersShared')->findOneBy(array('order_id' => $order_id));
        $this->em->remove($order);
        $this->em->flush();
    }
}
?>
